//
//  FLLGreenHandViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/3.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLGreenHandViewController.h"
#import "Masonry.h"
#import "FLLMacro.h"
#import "FLLGreenHandCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FLLGreenHandModel.h"
#import "GreenHandData.h"
#import "FLLGreenHandTopModel.h"
#import "TopData.h"
#import "TopLogos.h"
#import "UIImageView+WebCache.h"
#import "FLLGreenHandTableViewCell.h"
#import "GreenHandSecData.h"
#import "FLLMoreTableViewCell.h"
@interface FLLGreenHandViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *greenHandTableView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UICollectionView *greenHandCollectionView;
@property (nonatomic, strong) FLLGreenHandTopModel *fllGreenHandTopModel;
@property (nonatomic, strong) FLLGreenHandModel *fllGreenHandModel;
@property (nonatomic, strong) NSArray<TopLogos *> *topArray;
@property (nonatomic, assign) NSInteger selectedNmuber;
/* 下面的tableView数据源 */
@property (nonatomic, strong) NSMutableArray<GreenHandSecData *> *tableMutArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger itemIndex;
@property (nonatomic, assign) NSInteger totalCount;
@property (nonatomic, assign) NSInteger currentCount;
@property (nonatomic, assign) NSInteger type;
@end

@implementation FLLGreenHandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createPOSTRequest];
    [self createGreenHandTableView];
    [self createGreenHandCollectionView];
    [self createShareBarButton];
    self.selectedNmuber = 0;
    self.itemIndex = 0;
    self.type = 1;
    
}
- (void)createShareBarButton
{

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"分享"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickedShareButton:)];
}
// 分享按钮
- (void)didClickedShareButton:(UIBarButtonItem *)button
{

    
}
- (void)createPOSTRequest
{
    // 顶部collectionView
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
 andBody:@"methodName=TourLogo&version=4.4&user_id=0&_time=&_signature=" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.fllGreenHandTopModel = [FLLGreenHandTopModel modelWithDic:result];
     
       // 含有id的数组
        self.topArray = self.fllGreenHandTopModel.data.logos;
        [self.greenHandTableView reloadData];
        [self.greenHandCollectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    // 下面tableView
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
andBody:@"methodName=TourIndex&version=4.4&page=1&size=10&type=1&catid=5&user_id=0&_time=&_signature=" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
    
    self.fllGreenHandModel = [FLLGreenHandModel modelWithDic:result];
    self.tableMutArray = (NSMutableArray *)self.fllGreenHandModel.data.data;
    self.currentPage = 1;
    self.totalCount = [self.fllGreenHandModel.data.total integerValue];
    [self.greenHandTableView reloadData];
    
    } andFailureBlock:^(NSError *error) {
                 
             }];


    
}
- (void)createGreenHandTableView
{

    self.greenHandTableView = [[UITableView alloc]init];
    [self.view addSubview:_greenHandTableView];
    [_greenHandTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(100 * kHeightScale);
    }];
    _greenHandTableView.delegate = self;
    _greenHandTableView.dataSource = self;
    _greenHandTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_greenHandTableView registerNib:[UINib nibWithNibName:@"FLLGreenHandTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"greenHandTableCell"];
    [_greenHandTableView registerNib:[UINib nibWithNibName:@"FLLMoreTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"moreTableCell"];
    

}
- (void)createGreenHandCollectionView
{

    self.topView = [[UIView alloc] init];
    [self.view addSubview:_topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(100 * kHeightScale);
    }];
    
    UICollectionViewFlowLayout *greenHandLayout = [[UICollectionViewFlowLayout alloc] init];
    greenHandLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    greenHandLayout.minimumInteritemSpacing = 0;
    greenHandLayout.minimumLineSpacing = 0;
    greenHandLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.greenHandCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:greenHandLayout];
    [_topView addSubview:_greenHandCollectionView];
    [_greenHandCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.mas_equalTo(0);
    }];
    _greenHandCollectionView.delegate = self;
    _greenHandCollectionView.dataSource = self;
    _greenHandCollectionView.backgroundColor = [UIColor whiteColor];
    _greenHandCollectionView.showsHorizontalScrollIndicator = NO;
    [_greenHandCollectionView registerNib:[UINib nibWithNibName:@"FLLGreenHandCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"greenHandCollectionCell"];
    
    
}
#pragma mark -- TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _tableMutArray.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == _tableMutArray.count) {
        FLLMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreTableCell"];
        self.currentCount = _totalCount - _currentPage * 10;
        cell.grayLabel.text = [NSString stringWithFormat:@"还剩下%ld篇", _currentCount];
        cell.grayLabel.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1.00];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
        return cell;
    }
    FLLGreenHandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"greenHandTableCell"];
    [cell.dishImageVIew sd_setImageWithURL:[NSURL URLWithString:_tableMutArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    if (self.itemIndex == 0) {
        [cell.videoButton setImage:[UIImage imageNamed:@"play-A"] forState:UIControlStateNormal];
    } else {
    
        [cell.videoButton setImage:[UIImage new] forState:UIControlStateNormal];

    }
    cell.titleLabel.text = _tableMutArray[indexPath.row].title;
    cell.titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    cell.viewsCount.text = _tableMutArray[indexPath.row].views;
    cell.viewsCount.font = [UIFont systemFontOfSize:14 * kWidthScale];
    cell.lineLabel.backgroundColor = FLLLightGrayColor;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == _tableMutArray.count) {
        if (_currentCount <= 0) {
            return 0;
        }
        return 60 * kHeightScale;
    }
    return 260 * kHeightScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == _tableMutArray.count) {
        
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
        andBody:[NSString stringWithFormat:@"methodName=TourIndex&version=4.4&page=%ld&size=10&type=%ld&catid=%@&user_id=0&_time=&_signature=", (long)_currentPage + 1, (long)_type,  _topArray[_itemIndex].id_Data] andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            
            _currentPage++;
            self.fllGreenHandModel = [FLLGreenHandModel modelWithDic:result];
            [self.tableMutArray addObjectsFromArray:self.fllGreenHandModel.data.data];
            [self.greenHandTableView reloadData];
            [self.greenHandCollectionView reloadData];
                 } andFailureBlock:^(NSError *error) {
                     
                 }];

    } else {
    
        
        
    }
}
#pragma mark -- CollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.fllGreenHandTopModel.data.logos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLGreenHandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"greenHandCollectionCell" forIndexPath:indexPath];
    [cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:_topArray[indexPath.row].logo] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    }];
    cell.itemNameLabel.text = _topArray[indexPath.row].name;
    cell.itemNameLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    // 改变选中item字体颜色
    if (self.selectedNmuber == indexPath.row) {
        cell.itemNameLabel.textColor = FLLOrangeColor;
    } else {
        cell.itemNameLabel.textColor = [UIColor blackColor];
    }

    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat itemWidth = _greenHandCollectionView.bounds.size.width / 4.5;
    CGFloat itemHeight = _greenHandCollectionView.bounds.size.height;
    return CGSizeMake(itemWidth, itemHeight);
}
// collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    _itemIndex = indexPath.row;
    NSInteger index = indexPath.row;
    self.selectedNmuber = index;
    [self.tableMutArray removeAllObjects];
    _totalCount = 0;
    _currentPage = 1;
    _currentCount = 0;
    if (indexPath.row == 0) {
        _type = 1;
        [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
andBody:@"methodName=TourIndex&version=4.4&page=1&size=10&type=1&catid=5&user_id=0&_time=&_signature=" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
    
    self.fllGreenHandModel = [FLLGreenHandModel modelWithDic:result];
    self.tableMutArray = (NSMutableArray *)self.fllGreenHandModel.data.data;
    _totalCount = [self.fllGreenHandModel.data.total integerValue];

    [self.greenHandTableView reloadData];
    [self.greenHandCollectionView reloadData];
                } andFailureBlock:^(NSError *error) {
                     
        }];
    } else {
    
    _type = 2;
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
andBody:[NSString stringWithFormat:@"methodName=TourIndex&version=4.4&page=1&size=10&type=2&catid=%@&user_id=0&_time=&_signature=", _topArray[indexPath.row].id_Data] andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
    self.fllGreenHandModel = [FLLGreenHandModel modelWithDic:result];
    self.tableMutArray = (NSMutableArray *)self.fllGreenHandModel.data.data;
    _totalCount = [self.fllGreenHandModel.data.total integerValue];
         [self.greenHandTableView reloadData];
         [self.greenHandCollectionView reloadData];
             } andFailureBlock:^(NSError *error) {
                 
             }];
    }

}
- (void)setSelectedNmuber:(NSInteger)selectedNmuber
{

    _selectedNmuber = selectedNmuber;
    [self.greenHandCollectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
